package ltd.followkba.comment;

public class RC4 {

        public static byte[] decrypt(byte[] data, byte[] key) {
            if (data == null || key == null) {
                return null;
            }
            return RC4Base(data, key);
        }


        public static byte[] encrypt(byte[] data, byte[] key) {
            if (data == null || key == null) {
                return null;
            }
            return RC4Base(data, key);
        }



        private static byte[] initKey(byte[] aKey) {
            byte state[] = new byte[256];
            for (int i = 0; i < 256; i++) {
                state[i] = (byte) i;
            }
            int index1 = 0;
            int index2 = 0;
            if (aKey == null || aKey.length == 0) {
                return null;
            }
            for (int i = 0; i < 256; i++) {
                index2 = ((aKey[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff;
                byte tmp = state[i];
                state[i] = state[index2];
                state[index2] = tmp;
                index1 = (index1 + 1) % aKey.length;
            }
            return state;
        }

        private static byte[] RC4Base (byte [] input, byte[] mKkey) {
            int x = 0;
            int y = 0;
            byte key[] = initKey(mKkey);
            int xorIndex;
            byte[] result = new byte[input.length];

            for (int i = 0; i < input.length; i++) {
                x = (x + 1) & 0xff;
                y = ((key[x] & 0xff) + y) & 0xff;
                byte tmp = key[x];
                key[x] = key[y];
                key[y] = tmp;
                xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff;
                result[i] = (byte) (input[i] ^ key[xorIndex]);
            }
            return result;
        }
    }

